package net.bluemind.ysnp.impl;

import java.io.IOException;

import net.bluemind.unixsocket.UnixServerSocket;
import net.bluemind.ysnp.YSNPConfiguration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Wires all the daemon components together
 * 
 * @author tom
 * 
 */
public class AuthChainBuilder {

	private final Logger logger = LoggerFactory.getLogger(getClass());
	private UnixServerSocket socket;
	private MainLoop mainLoop;
	private ConnectionHandler handler;
	private YSNPConfiguration conf;
	private ValidationPolicy vp;

	public AuthChainBuilder(YSNPConfiguration conf, UnixServerSocket socket) {
		this.socket = socket;
		this.conf = conf;
		this.vp = new ValidationPolicy(conf);
	}

	public void start() {
		handler = new ConnectionHandler(vp, conf);
		mainLoop = new MainLoop(socket, handler);
		logger.info("Starting main loop");
		mainLoop.start();
	}

	public void shutdown() throws IOException {
		mainLoop.shutdown();
		handler.shutdown();
	}

}
